::Author:Dzlua
::QQ:505544956
::Time:2017/03/09
@echo off

::支持参数 ansi,static,表示使用的spdui库的类型
::debug [target] [ansi] [static]
::debug r target

::运行
if "%1"=="r" goto RUN
if "%1"=="-h" goto HELPME

::编译所有的demos
:CONFIG

set rebuild=build
set onlybuildlua=false
set target=
::设置 -r,target
if "%1"=="-r" (
    set "rebuild=-r"
    if not "%2"=="ansi" (
        if not "%2"=="static" (
            set "target=%2"
        )
    )
) else if "%1"=="-l" (
    set "onlybuildlua=true"
    if not "%2"=="ansi" (
        if not "%2"=="static" (
            set "target=%2"
        )
    )
) else (
    if not "%1"=="ansi" (
        if not "%1"=="static" (
            set "target=%1"
        )
    )
)

::设置
set unicode=true
set shared=true
for %%c in (%1,%2,%3,%4) do (
    if "%%c"=="ansi" set "unicode=false"
    if "%%c"=="static" set "shared=false"
)

::设置targetspdui
set targetspdui=spdui_d
if %unicode%==true (
    if %shared%==true (
        set "targetspdui=spdui_d"
    ) else (
        set "targetspdui=spdui_sd"
    )
) else (
    if %shared%==true (
        set "targetspdui=spdui_ad"
    ) else (
        set "targetspdui=spdui_asd"
    )
)

::分配动作
if %rebuild%==-r goto BUILD_SPDUI
if %onlybuildlua%==true goto BUILD_LUA4SPDUI
goto BUILD_TARGET

:BUILD_SPDUI
::编译库
xmake f -m debug -F spdui.lua
xmake %rebuild% -F spdui.lua %targetspdui%

::创建目录
if not exist "out/bin/" md "out/bin"
if not exist "out/lib/" md "out/lib"
::复制.lib文件到lib
for /R "build" %%f in (spdui*.lib,spdui*.dll) do copy %%f "out/lib/%%~nxf"
::复制.dll文件到bin
for /R "build" %%f in (spdui*.dll) do copy %%f "out/bin/%%~nxf"

:BUILD_LUA4SPDUI
::编译库
xmake f -m debug -F lua4spdui.lua
xmake %rebuild% -F lua4spdui.lua lua4%targetspdui%

::复制.lib文件到lib
for /R "build" %%f in (lua4spdui*.lib,lua4spdui*.dll) do copy %%f "out/lib/%%~nxf"
for /R "pkg\LuaPlus\lib" %%f in (*.dll) do copy %%f "out/lib/%%~nxf"
::复制.dll文件到bin
for /R "build" %%f in (lua4spdui*.dll) do copy %%f "out/bin/%%~nxf"
for /R "pkg\LuaPlus\lib" %%f in (*.dll) do copy %%f "out/bin/%%~nxf"

:BUILD_TARGET
::编译目标
xmake f -m debug --spdui-unicode=%unicode% --spdui-shared=%shared% -F demos.lua
xmake %rebuild% -F demos.lua  %target%

::复制所有.exe
for /R "build" %%f in (*.exe) do copy %%f "out/bin/%%~nxf"
::复制hello资源到out/bin
xcopy /d /s /i /h /y "demos\hello\skin" "out\bin\skin\hello\"
xcopy /d /s /i /h /y "demos\sdtool\skin" "out\bin\skin\sdtool\"

xcopy /d /s /i /h /y "demos\testlua\skin" "out\bin\skin\testlua\"
xcopy /d /s /i /h /y "demos\testlua\lua" "out\bin\lua\testlua\"

goto END

::运行demo
:RUN
out\bin\%2.exe
goto END

:HELPME
echo ...
echo    Usages: [action] [target] [ansi] [static]
echo    actions:
echo        1. -h : show this message.
echo        2. r : run the target.
echo        3. -r : rebuild spdui and target.ifnot target,rebuild all targets.
echo        4. -l : build lua4spdui and target.ifnot target,rebuild all targets.
echo        5. noaction : build target.ifnot target,build all.

goto END

:END